package com.ywjt.platformbusiness.my.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.base.BaseModel;
import com.ywjt.platformbusiness.common.UserData;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.util.JsonUtils;
import com.ywjt.platformbusiness.util.SPUtils;
import com.ywjt.platformbusiness.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import static com.ywjt.platformbusiness.common.MethodUtils.getUserId;

public class ActivityNickNameModify extends BaseActivity implements View.OnClickListener {
    private EditText tvNickName;
    private String nickname="";
    private TextView tvSureModify;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ActivityNickNameModify.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvSureModify:
                if (nickname.equals("")){
                    ToastUtil toastUtil=new ToastUtil(ActivityNickNameModify.this,R.layout.popu_success1,"请输入要修改的昵称",1);
                    toastUtil.show();
                    return;
                }else {
                    modifyNickName();
                }

                break;
        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_nicknamemodify;
    }

    @Override
    protected void initView() {
        setTitleText("昵称修改");
        tvSureModify=findViewById(R.id.tvSureModify);
        tvNickName=findViewById(R.id.tvNickName);
        tvNickName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                    nickname=s.toString().trim();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        tvSureModify.setOnClickListener(this::onClick);
    }

    @Override
    protected void initData() {

    }

    /**
     * 获取要使用的标题栏样式(此函数由子类重写)
     *
     * @return
     */
    @Override
    protected int getTitleBarType() {
        return 0;
    }

    //修改昵称
    public void modifyNickName(){

        new HttpRequest(getContext()).doGet(UrlConstants.login_modifyNickName+nickname, "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {

                String data = result.toString();
                //把请求得数据转换成json对象
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), BaseModel.class);
                        if (obj instanceof BaseModel) {
                            BaseModel model = (BaseModel) obj;
                            UserData.create(ActivityNickNameModify.this).changeUserData(UserData.NICKNAME,nickname);
                            finish();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }

}
